package com.edu.app.controller.linkcontrollers;

import com.edu.app.bean.core.SysRole;
import com.edu.app.bean.linkbeans.SysUserRole;
import com.edu.app.constant.ErrorConstant;
import com.edu.app.constant.StaticConstant;
import com.edu.app.constant.SysMessage;
import com.edu.app.service.sys.linkservices.SysUserRoleService;
import com.edu.app.util.ErrorUtil;
import com.edu.app.util.JWTUtil;
import com.edu.app.util.MsgUtil;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping(value = "/api/user_roles")
@RestController
public class SysUserRoleController {

    @Autowired
    SysUserRoleService sysUserRoleService;

    //使用get需要添加默认get函数
    @GetMapping(value = "")
    @RequiresAuthentication
    public ModelMap getUserRoleList(){
        List<SysUserRole> sysUserRoleList = sysUserRoleService.getObjectList();
        return MsgUtil.retMsg(SysMessage.GET_USER_ROLE_SUCCEED, StaticConstant.USER_ROLE_LIST, sysUserRoleList);
    }

    @GetMapping(value = "/{user_id}")
    @RequiresAuthentication
    public ModelMap getCurrentUserRoleList(@PathVariable Integer user_id){
        List<SysRole> sysRoleList = sysUserRoleService.getRoleListByUserId(user_id);
        return MsgUtil.retMsg(SysMessage.GET_USER_ROLE_SUCCEED, StaticConstant.ROLE_LIST, sysRoleList);
    }
    /*
     * 为User添加角色
     * http://localhost:8080/api/user_roles?user_id=1&role_id=1
     * */
    @PostMapping(value = "")
    @RequiresRoles(value = {StaticConstant.ADMIN_ROLE})
    public ModelMap addUserRole(@RequestHeader("Authorization") String token, @RequestParam Integer user_id, @RequestParam Integer role_id){

        try{
            SysUserRole sysUserRole = new SysUserRole(user_id, role_id);
            sysUserRole.initializeSystemColumns(JWTUtil.getUsername(token));
            Integer r = sysUserRoleService.add(sysUserRole);
            if(r > 0){
                return MsgUtil.retMsg(SysMessage.ADD_USER_ROLE_SUCCEED, "user_role",sysUserRole);
            }else{
                return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
            }
        }catch (org.springframework.dao.DuplicateKeyException e){
            return ErrorUtil.retErrMsg(ErrorConstant.USER_ROLE_ALREADY_EXISTS);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    /*
     * 删除用户角色
     * http://localhost:8080/api/deleteUserRole?user_id=1&role_id=1
     * */
    @DeleteMapping(value = "")
    @RequiresRoles(value = {StaticConstant.ADMIN_ROLE})
    public ModelMap deleteUserRole(@RequestParam Integer user_id, @RequestParam Integer role_id){

        SysUserRole sysUserRole = sysUserRoleService.getUserRole(user_id, role_id);

        if(sysUserRole != null){
            try{
                Integer r = sysUserRoleService.delete(sysUserRole.getId());
                if(r > 0){
                    return MsgUtil.retMsg(SysMessage.DELETE_USER_ROLE_SUCCEED, null ,null);
                }else{
                    return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
                }
            }catch (Exception e){
                return ErrorUtil.retErrMsg(e);
            }
        }else{
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }

    }
}
